package com.lq.dao;

import com.lq.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//这个不是底层的Dao,是子类Dao,把共有的操作放到了父类中，特有的操作作为子类
public class UserDao extends BaseDao{
    //保存用户
    public int save(User user) {
        String sql = "insert into user(username,password,profile) values(?,?,?)";
        //初始化连接，交互
        PreparedStatement statement = null;
        Connection conn = getConn();
        try {
            statement = conn.prepareStatement(sql);

            statement.setString(1,user.getUsername());
            statement.setString(2,user.getPassword());
            statement.setString(3,user.getProfile());

            System.out.println(statement);

            //执行的结果返回是个int值代表所影响的行数
            return statement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            closeAll(conn,statement,null);
        }
    }

    //重名的用户可能不止一个所以这个使用集合
    public List<User> findUserByName(String username) {
        String sql = "select id,username,password,profile from user where username=?";
        //初始化连接，交互，查询结果
        PreparedStatement statement = null;
        Connection conn = getConn();
        ResultSet resultSet = null;
        List<User> users = new ArrayList<User>();
        try {
            statement = conn.prepareStatement(sql);

            statement.setString(1,username);

            System.out.println(statement);

            resultSet = statement.executeQuery();

            while (resultSet.next()){
//                int id = resultSet.getInt("id");
                String name = resultSet.getString("username");
                String password = resultSet.getString("password");
                String profile = resultSet.getString("profile");

                User user = new User(name, password, profile);
                users.add(user);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            closeAll(conn,statement,resultSet);
        }
        return users;
    }





    //获取所有的用户
    public List<User> findALLUsers() {
        String sql = "select id,username,password,profile from user";
        PreparedStatement statement = null;
        Connection conn = getConn();
        ResultSet resultSet = null;
        List<User> users = new ArrayList<>();
        try {
            statement = conn.prepareStatement(sql);

            System.out.println(statement);
            resultSet = statement.executeQuery();

            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("username");
                String password = resultSet.getString("password");
                String profile = resultSet.getString("profile");
                User user = new User(name, password, profile);
                user.setId(id);
                users.add(user);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            closeAll(conn,statement,resultSet);
        }
        return users;
    }


    public int deleteById(String id) {
        String sql = "delete from user where id=?";
        PreparedStatement statement = null;
        Connection conn = getConn();
        try {
            statement = conn.prepareStatement(sql);

            statement.setString(1,id);

            System.out.println(statement);
            return statement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            closeAll(conn,statement,null);
        }
    }


    public User findUserByid(Integer id) {
        String sql = "select id,username,password,profile from user where id=?";
        PreparedStatement statement = null;
        Connection conn = getConn();
        ResultSet resultSet = null;
        try {
            statement = conn.prepareStatement(sql);

            statement.setInt(1,id);

            System.out.println(statement);
            resultSet = statement.executeQuery();

            while (resultSet.next()){
                String name = resultSet.getString("username");
                String password = resultSet.getString("password");
                String profile = resultSet.getString("profile");

                User user = new User(name, password, profile);
                user.setId(id);
                return user;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            closeAll(conn,statement,resultSet);
        }
        return null;
    }


    public Integer update(User user) {
        String sql = "update user set username = ?,profile=?where id=?";
        PreparedStatement statement = null;
        Connection conn = getConn();
        try {
            statement = conn.prepareStatement(sql);

            statement.setString(1,user.getUsername());
            statement.setString(2,user.getProfile());
            statement.setInt(3,user.getId());

            return statement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            closeAll(conn,statement,null);
        }
    }
}
